package or.exa.cangguan.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import or.exa.cangguan.common.Page;
import or.exa.cangguan.common.Result;
import or.exa.cangguan.entity.dto.GoodsDto;
import or.exa.cangguan.entity.vo.OutStoreVo;
import or.exa.cangguan.service.OutStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/outStore")
@Api(tags = "出库单管理")
public class OutStoreController {

    @Autowired
    private OutStoreService outStoreService;

    @PostMapping("/add")
    @ApiOperation("创建出库单")
    public Result addOutStore(@RequestBody OutStoreVo outStoreVo) {
        int i = outStoreService.addOutStore(outStoreVo);
        return i > 0 ? Result.ok("添加成功") : Result.ok("添加失败");
    }

    @PostMapping("/search")
    @ApiOperation("分页查询出库单")
    public Result searchOutStore(@RequestBody OutStoreVo outStoreVo) {
        Page page = outStoreService.searchOutStore(outStoreVo);
        return Result.ok(page);
    }

    @PostMapping    ("/goods/{id}")
    @ApiOperation("查询出库单商品")
    public Result searchOutStoreGoods(@PathVariable Integer id) {
        List<GoodsDto> goodsDtos = outStoreService.searchOutStoreGoods(id);
        return Result.ok(goodsDtos);
    }

    @PostMapping("/update")
    @ApiOperation("更新出库单")
    public Result updateOutStore(@RequestBody OutStoreVo outStoreVo) {
        int i = outStoreService.updateOutStore(outStoreVo);
        return i > 0 ? Result.ok("更新成功") : Result.ok("更新失败");
    }

    @PostMapping("/updateStatus")
    @ApiOperation("更新出库单状态")
    public Result updateStatus(@RequestBody OutStoreVo outStoreVo) {
        log.info(outStoreVo.toString());
        int i = outStoreService.updateStatus(outStoreVo.getOutId(), outStoreVo.getOutStatus(), outStoreVo.getOutPlanTime());
        return i > 0 ? Result.ok("更新状态成功") : Result.ok("更新状态失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除出库单")
    public Result deleteOutStore(@PathVariable Integer id) {
        int i = outStoreService.deleteOutStore(id);
        return i > 0 ? Result.ok("删除成功") : Result.ok("删除失败");
    }
}
